﻿using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.MSSQLDAL
{
   public class AmbulanceStateDAL
    {
        public static readonly SqlConnectionStringBuilder DispatchBuilder = new SqlConnectionStringBuilder(SqlHelper.AttemperConnectionString);//取调度库的链接字符串
        public static readonly string DispatchDatabase = DispatchBuilder.InitialCatalog;
        #region

        public object GetAmbulanceStateList(int pageSize, int pageIndex, string sortName, string sortOrder, string center, string station, string actualNumber, string ambulanceNumber, string driver, string doctor
                                   , string nurse, string stretcher, string workState, E_StatisticsPermisson em, string selfWorkCode, string selfCenterID, string selfStationID, string AmbCode, string eventDispatchCenterID)
        {
            List<AmbulanceStateInfo> list = new List<AmbulanceStateInfo>();
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
select distinct identity(int,1,1) as 行号,事件编码=tt.事件编码,分中心=tc.名称,分站=ts.名称,车辆类型=tzat.名称,工作状态=tzas.名称
,生成任务时刻=tt.生成任务时刻,车辆等级=tzl.名称,命令单发送去向=tzc.名称,分组=tzg.名称                       
,ta.*
into #temp 
from TAmbulance ta 
left join TZAmbulanceType tzat on tzat.编码=ta.车辆类型编码
left join TZAmbulanceState tzas on tzas.编码= ta.工作状态编码
left join TZAmbulanceLevel tzl on tzl.编码=ta.车辆等级编码
left join TZAmbulanceGroup tzg on tzg.编码=ta.分组编码
left join TZCommandAspect tzc on tzc.编码=ta.命令单发送去向编码
left join TTask tt on tt.任务编码=ta.任务编码
left join TStation ts on ta.分站编码=ts.编码
left join TCenter tc on ts.中心编码=tc.编码 "); 
 if (em == E_StatisticsPermisson.SELF)
            {
                sb.Append(@"
left join TTaskPersonLink ttpl on ttpl.任务编码=tt.任务编码
left join TPerson tp on tp.编码=ttpl.人员编码");
            }
 sb.Append(@"                     
where 1=1 and ta.是否有效=1");
            WhereClauseUtility.AddInSelectQuery("ta.实际标识", actualNumber, sb);
            WhereClauseUtility.AddStringLike("ta.车牌号码", ambulanceNumber, sb);            
            WhereClauseUtility.AddStringEqual("ta.工作状态编码", workState, sb);
            WhereClauseUtility.AddStringLike("司机", driver, sb);
            WhereClauseUtility.AddStringLike("医生", doctor, sb);
            WhereClauseUtility.AddStringLike("护士", nurse, sb);
            WhereClauseUtility.AddStringLike("担架工", stretcher, sb);
            WhereClauseUtility.AddStringLike("ta.车辆编码", AmbCode, sb);

            if (eventDispatchCenterID == "1")
                sb.Append(" and ts.中心编码<=6 ");
            else
                WhereClauseUtility.AddStringEqual("ts.中心编码", eventDispatchCenterID, sb); //2020-11-22 zch

            if (em == E_StatisticsPermisson.None)
                return null;
            else 
            {
                if (em == E_StatisticsPermisson.ALL)
            {
                WhereClauseUtility.AddInSelectQuery("ts.中心编码", center, sb);
                WhereClauseUtility.AddInSelectQuery("tt.分站编码", station, sb);
                if (sortName != "生成任务时刻")
                {
                    sb.Append(@"  
                    order by  " + sortName + "  " + sortOrder + "  ,tt.生成任务时刻  desc ");
                }
                else {
                    sb.Append(@"  
                    order by  " + sortName + "  "+sortOrder+"  ");
                }                     
            }
            else if (em == E_StatisticsPermisson.CENTER)
            {
                WhereClauseUtility.AddStringEqual("ts.中心编码", selfCenterID, sb);
                WhereClauseUtility.AddInSelectQuery("tt.分站编码", station, sb);
            
                if (sortName != "生成任务时刻")
                {
                    sb.Append(@"  
                    order by  " + sortName + "  " + sortOrder + "  ,tt.生成任务时刻  desc ");
                }
                else {
                    sb.Append(@"  
                    order by  " + sortName + "  "+sortOrder+"  ");
                }             
            }
            else if (em == E_StatisticsPermisson.STATION)
            {
                WhereClauseUtility.AddStringEqual("tt.分站编码", selfStationID, sb);              

                if (sortName != "生成任务时刻")
                {
                    sb.Append(@"  
                    order by  " + sortName + "  " + sortOrder + "  ,tt.生成任务时刻  desc ");
                }
                else {
                    sb.Append(@"  
                    order by  " + sortName + "  "+sortOrder+"  ");
                }           
            }
            else if (em == E_StatisticsPermisson.SELF)
            {
                sb.Append("and tp.工号 = '" + selfWorkCode + @"' ");

               if (sortName != "生成任务时刻")
                {
                    sb.Append(@"  
                    order by  " + sortName + "  " + sortOrder + "  ,tt.生成任务时刻  desc ");
                }
                else {
                    sb.Append(@"  
                    order by  " + sortName + "  "+sortOrder+"  ");
                } 
            }
                sb.Append(@"
                    select top " + pageSize + " A.*  from #temp A where 行号>" + (pageIndex - 1) * pageSize + @" order by 行号 
               
                    SELECT count(*) FROM #temp t
                    drop table #temp ");
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        AmbulanceStateInfo info = new AmbulanceStateInfo();
                        info.center = dr["分中心"].ToString();
                        info.station = dr["分站"].ToString();
                        info.ambulanceType = dr["车辆类型"].ToString();
                        info.actualNumber = dr["实际标识"].ToString();
                        info.ambulanceNumber = dr["车牌号码"].ToString();
                        info.workState = dr["工作状态"].ToString();
                        info.driver = dr["司机"].ToString();
                        info.doctor = dr["医生"].ToString();
                        info.nurse = dr["护士"].ToString();
                        info.stretcher = dr["担架工"].ToString();
                        info.taskGenerationTime = dr["生成任务时刻"].ToString();
                        info.EventCode = dr["事件编码"].ToString();
                        info.AmbCode=dr["车辆编码"].ToString();
                        info.OrderSingleSend=dr["命令单发送去向"].ToString();
                        info.TaskCode = dr["任务编码"].ToString();
                        info.TaskNumber = dr["任务流水号"].ToString();
                        info.UserNumber = dr["用户流水号"].ToString();
                        info.PerformCount = dr["当班执行任务数"].ToString();
                        info.Rescue = dr["抢救员"].ToString();
                        info.KeyTime = dr["按键时刻"].ToString();
                        info.Tel = dr["随车电话"].ToString();
                        info.Group = dr["分组"].ToString();
                        info.ShunXuNumber = dr["顺序号"].ToString();
                        info.DeskNumber = dr["占用台号"].ToString();
                        info.AmbLevel = dr["车辆等级"].ToString();
                        info.IFLine = Convert.ToBoolean(dr["是否在线"]);
                        info.IFActive = Convert.ToBoolean(dr["是否有效"]);
                        list.Add(info);
                    }
                }
                int total = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                var result = new { total = total, rows = list };
                return result;
            }       
        }


        //获取车辆工作状态
        public object GetWorkStateList()
        {
            List<CheckModelExt> list = new List<CheckModelExt>();
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select 编码,名称 from TZAmbulanceState where 是否有效 = 1  ");
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    CheckModelExt info = new CheckModelExt();
                    info.ID = dr["编码"].ToString();
                    info.Name = dr["名称"].ToString();
                    list.Add(info);
                }
            }
            return list;
        }

        public AmbulanceStateInfo GetAmbulance(string AmbCode, string eventDispatchCenterID)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@" 
                select 事件编码=tt.事件编码,分中心=tc.名称,分站=ts.名称,车辆类型=tzat.名称,工作状态=tzas.名称
                ,生成任务时刻=tt.生成任务时刻,车辆等级=tzl.名称,命令单发送去向=tzc.名称,分组=tzg.名称,ta.是否有效,ta.是否在线                       
                ,ta.*
                from TAmbulance ta 
                left join TZAmbulanceType tzat on tzat.编码=ta.车辆类型编码
                left join TZAmbulanceState tzas on tzas.编码= ta.工作状态编码
                left join TZAmbulanceLevel tzl on tzl.编码=ta.车辆等级编码
                left join TZAmbulanceGroup tzg on tzg.编码=ta.分组编码
                left join TZCommandAspect tzc on tzc.编码=ta.命令单发送去向编码
                left join TTask tt on tt.任务编码=ta.任务编码
                left join TStation ts on ta.分站编码=ts.编码
                left join TCenter tc on ts.中心编码=tc.编码
                        where ta.车辆编码='" + AmbCode + @"'
                ");
            if (eventDispatchCenterID == "1")
                sb.Append(" and ts.中心编码<=6 ");
            else
                WhereClauseUtility.AddStringEqual("ts.中心编码", eventDispatchCenterID, sb); //2020-11-22 zch
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
            {
                AmbulanceStateInfo TAinfo = new AmbulanceStateInfo();
                if (dr.Read())
                {
                    TAinfo.TaskCode = Convert.ToString(dr["任务编码"]);
                    TAinfo.EventCode = Convert.ToString(dr["事件编码"]);
                    TAinfo.AmbCode = Convert.ToString(dr["车辆编码"]);
                    TAinfo.TaskNumber = Convert.ToString(dr["任务流水号"]);
                    TAinfo.UserNumber = Convert.ToString(dr["用户流水号"]);

                    TAinfo.center = Convert.ToString(dr["分中心"]);
                    TAinfo.station = Convert.ToString(dr["分站"]);
                    TAinfo.ambulanceType = Convert.ToString(dr["车辆类型"]);
                    TAinfo.ambulanceNumber = Convert.ToString(dr["车牌号码"]);
                    TAinfo.actualNumber = Convert.ToString(dr["实际标识"]);
                    TAinfo.workState = Convert.ToString(dr["工作状态"]);
                    TAinfo.driver = Convert.ToString(dr["司机"]);
                    TAinfo.doctor = Convert.ToString(dr["医生"]);
                    TAinfo.nurse = Convert.ToString(dr["护士"]);
                    TAinfo.stretcher = Convert.ToString(dr["担架工"]);

                    TAinfo.Rescue = Convert.ToString(dr["抢救员"]);
                    TAinfo.Tel = Convert.ToString(dr["随车电话"]);
                    TAinfo.Group = Convert.ToString(dr["分组"]);
                    TAinfo.AmbLevel = Convert.ToString(dr["车辆等级"]);
                    TAinfo.IFLine = Convert.ToBoolean(dr["是否在线"]);
                    TAinfo.ShunXuNumber = Convert.ToString(dr["顺序号"]);
                    TAinfo.IFActive = Convert.ToBoolean(dr["是否有效"]);
                }
                return TAinfo;

            }
        }

        #endregion
    }
}
